import { useState, useEffect } from 'react';
// 修改为使用静态数据，不调用真实API
// import { userApi, roleApi, departmentApi, teamApi } from '../../services';

// 统计数据类型
interface DashboardStats {
  totalUsers: number;
  activeUsers: number;
  totalRoles: number;
  totalDepartments: number;
  totalGroups: number;
  onlineUsers: number;
  securityScore: number;
}

// 系统指标类型
interface SystemMetric {
  name: string;
  value: number;
  status: 'normal' | 'warning' | 'error';
  color: string;
  icon: React.ReactNode;
  trend: string;
}

// 快速操作类型
interface QuickAction {
  title: string;
  description: string;
  icon: React.ReactNode;
  color: string;
  bgColor: string;
  path: string;
  count: number;
}

// 最近活动类型
interface RecentActivity {
  time: string;
  action: string;
  type: 'info' | 'warning' | 'error';
  user: string;
  detail: string;
}

// 热门功能类型
interface PopularFeature {
  name: string;
  usage: number;
  icon: React.ReactNode;
  trend: 'up' | 'down';
}

// 安全告警类型
interface SecurityAlert {
  type: 'warning' | 'info' | 'error';
  message: string;
  time: string;
  action: string;
}

// Dashboard统计数据hook
export const useDashboardStats = () => {
  const [stats, setStats] = useState<DashboardStats>({
    totalUsers: 0,
    activeUsers: 0,
    totalRoles: 0,
    totalDepartments: 0,
    totalGroups: 0,
    onlineUsers: 0,
    securityScore: 0,
  });
  const [loading, setLoading] = useState(false);

  const loadStats = async () => {
    try {
      setLoading(true);
      
      // 使用静态数据替代API调用
      await new Promise(resolve => setTimeout(resolve, 500)); // 模拟网络延迟
      
      const calculatedStats = {
        totalUsers: 1247,
        activeUsers: 892,
        totalRoles: 8,
        totalDepartments: 12,
        totalGroups: 15,
        onlineUsers: 156,
        securityScore: 85,
      };
      
      setStats(calculatedStats);
    } catch (error) {
      console.error('加载统计数据失败:', error);
      // 设置静态默认值
      setStats({
        totalUsers: 1247,
        activeUsers: 892,
        totalRoles: 8,
        totalDepartments: 12,
        totalGroups: 15,
        onlineUsers: 156,
        securityScore: 85,
      });
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadStats();
  }, []);

  return {
    stats,
    loading,
    refresh: loadStats,
  };
};

// 系统指标hook
export const useSystemMetrics = () => {
  const [metrics, setMetrics] = useState<SystemMetric[]>([]);
  const [loading, setLoading] = useState(false);

  const loadMetrics = async () => {
    try {
      setLoading(true);
      // 模拟网络延迟
      await new Promise(resolve => setTimeout(resolve, 300));
      
      const systemMetrics = [
        { 
          name: '用户活跃度', 
          value: 85, 
          status: 'normal' as const, 
          color: '#52c41a',
          icon: null, // 将在组件中设置
          trend: '+12%'
        },
        { 
          name: '权限合规性', 
          value: 92, 
          status: 'normal' as const, 
          color: '#1890ff',
          icon: null,
          trend: '+5%'
        },
        { 
          name: '系统安全性', 
          value: 78, 
          status: 'warning' as const, 
          color: '#faad14',
          icon: null,
          trend: '-3%'
        },
        { 
          name: '数据完整性', 
          value: 96, 
          status: 'normal' as const, 
          color: '#52c41a',
          icon: null,
          trend: '+1%'
        },
      ];
      
      setMetrics(systemMetrics);
    } catch (error) {
      console.error('加载系统指标失败:', error);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadMetrics();
  }, []);

  return {
    metrics,
    loading,
    refresh: loadMetrics,
  };
};

// 最近活动hook
export const useRecentActivities = () => {
  const [activities, setActivities] = useState<RecentActivity[]>([]);
  const [loading, setLoading] = useState(false);

  const loadActivities = async () => {
    try {
      setLoading(true);
      // 模拟网络延迟
      await new Promise(resolve => setTimeout(resolve, 400));
      
      const recentActivities = [
        { 
          time: '5分钟前', 
          action: '用户登录', 
          type: 'info' as const,
          user: '张三',
          detail: '从192.168.1.100登录系统'
        },
        { 
          time: '30分钟前', 
          action: '权限异常告警', 
          type: 'error' as const,
          user: '系统',
          detail: '检测到异常权限访问尝试'
        },
        { 
          time: '1小时前', 
          action: '角色权限更新', 
          type: 'info' as const,
          user: '管理员',
          detail: '更新了开发者角色权限'
        },
        { 
          time: '2小时前', 
          action: '新用户注册', 
          type: 'info' as const,
          user: '李四',
          detail: '新用户完成注册流程'
        },
      ];
      
      setActivities(recentActivities);
    } catch (error) {
      console.error('加载最近活动失败:', error);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadActivities();
  }, []);

  return {
    activities,
    loading,
    refresh: loadActivities,
  };
};

// 热门功能hook
export const usePopularFeatures = () => {
  const [features, setFeatures] = useState<PopularFeature[]>([]);
  const [loading, setLoading] = useState(false);

  const loadFeatures = async () => {
    try {
      setLoading(true);
      // 模拟网络延迟
      await new Promise(resolve => setTimeout(resolve, 350));
      
      const popularFeatures = [
        { 
          name: '用户管理', 
          usage: 89, 
          trend: 'up' as const,
          description: '用户信息管理和权限分配',
          icon: null
        },
        { 
          name: '角色管理', 
          usage: 76, 
          trend: 'up' as const,
          description: '角色权限配置和管理',
          icon: null
        },
        { 
          name: '部门管理', 
          usage: 65, 
          trend: 'down' as const,
          description: '组织架构和部门设置',
          icon: null
        },
        { 
          name: '系统监控', 
          usage: 58, 
          trend: 'up' as const,
          description: '系统运行状态监控',
          icon: null
        },
      ];
      
      setFeatures(popularFeatures);
    } catch (error) {
      console.error('加载热门功能失败:', error);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadFeatures();
  }, []);

  return {
    features,
    loading,
    refresh: loadFeatures,
  };
};

// 安全告警hook
export const useSecurityAlerts = () => {
  const [alerts, setAlerts] = useState<SecurityAlert[]>([]);
  const [loading, setLoading] = useState(false);

  const loadAlerts = async () => {
    try {
      setLoading(true);
      // 模拟网络延迟
      await new Promise(resolve => setTimeout(resolve, 300));
      
      const securityAlerts = [
        { 
          type: 'error' as const,
          message: '检测到来自异常IP的登录尝试', 
          time: '2分钟前',
          action: '立即处理'
        },
        { 
          type: 'warning' as const,
          message: '管理员角色权限发生变更', 
          time: '15分钟前',
          action: '查看详情'
        },
        { 
          type: 'info' as const,
          message: '系统CPU使用率超过80%', 
          time: '1小时前',
          action: '查看监控'
        },
      ];
      
      setAlerts(securityAlerts);
    } catch (error) {
      console.error('加载安全告警失败:', error);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadAlerts();
  }, []);

  return {
    alerts,
    loading,
    refresh: loadAlerts,
  };
};

// 综合Dashboard数据hook
export const useDashboard = () => {
  const statsHook = useDashboardStats();
  const metricsHook = useSystemMetrics();
  const activitiesHook = useRecentActivities();
  const featuresHook = usePopularFeatures();
  const alertsHook = useSecurityAlerts();

  const refreshAll = () => {
    statsHook.refresh();
    metricsHook.refresh();
    activitiesHook.refresh();
    featuresHook.refresh();
    alertsHook.refresh();
  };

  const loading = statsHook.loading || metricsHook.loading || 
                  activitiesHook.loading || featuresHook.loading || 
                  alertsHook.loading;

  return {
    stats: statsHook.stats,
    metrics: metricsHook.metrics,
    activities: activitiesHook.activities,
    features: featuresHook.features,
    alerts: alertsHook.alerts,
    loading,
    refreshAll,
  };
};